package com.mku.parkingmanagement_service.service.impl;

import cn.dev33.satoken.util.SaFoxUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mku.parkingmanagement_service.entity.Recharge;
import com.mku.parkingmanagement_service.entity.Stall;
import com.mku.parkingmanagement_service.entity.StallRes;
import com.mku.parkingmanagement_service.entity.User;
import com.mku.parkingmanagement_service.mapper.RechargeMapper;
import com.mku.parkingmanagement_service.mapper.StallMapper;
import com.mku.parkingmanagement_service.mapper.StallResMapper;
import com.mku.parkingmanagement_service.mapper.UserMapper;
import com.mku.parkingmanagement_service.service.StallService;
import com.mku.parkingmanagement_service.vo.StallResVo;
import com.mku.parkingmanagement_service.vo.StallVo;
import com.mku.parkingmanagement_service.vo.comm.ResultVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @className: StallServiceImpl
 * @author: Maoxx
 * @date: 2024/11/9 13:43
 * @Version: 1.0
 * @description:
 */

@Service
public class StallServiceImpl extends ServiceImpl<StallMapper, Stall> implements StallService {

    @Resource
    private StallMapper stallMapper;

    @Resource
    private StallResMapper stallResMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private RechargeMapper rechargeMapper;

    @Override
    public ResultVO<Object> getStallsByPage(StallVo stallVo) {
        Page<Stall> stallPage = new Page<>(stallVo.getPagenum(), stallVo.getPageSize());

        QueryWrapper<Stall> qw = new QueryWrapper<>();

        if (!SaFoxUtil.isEmpty(stallVo.getCarArea())) {
            qw.like("stall_area", stallVo.getCarArea());
        }

        if (!SaFoxUtil.isEmpty(stallVo.getCarType())) {
            qw.like("stall_type", stallVo.getCarType());
        }

        if (!SaFoxUtil.isEmpty(stallVo.getCarState())) {
            qw.like("stall_state", stallVo.getCarState());
        }

        return ResultVO.success(stallMapper.selectPage(stallPage, qw));
    }

    @Override
    public ResultVO<Object> addStall(Stall stall) {
        Stall one = stallMapper.selectOne(new QueryWrapper<Stall>().eq("stall_num", stall.getStallNum()));
        if (one != null) {
            return ResultVO.fail("车位编号已存在",null);
        }


        stall.setStallMoney(stall.getStallMoney() == null ? 3 : stall.getStallMoney());
        stall.setStallState(stall.getStallState() == null ? "空闲中" : stall.getStallState());
        stall.setStallLive(stall.getStallLive() == null ? "1" : stall.getStallLive());

        int insert = stallMapper.insert(stall);

        if (insert < 1) {
            return ResultVO.fail("车位添加失败",null);
        }
        return ResultVO.success();
    }

    @Override
    public Object getAllListStallRes(StallResVo stallResVo) {
        Page page = new Page(stallResVo.getPagenum(), stallResVo.getPageSize());
        return stallResMapper.getAllListStallRes(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVO<Object> orderStall(Integer uid, Integer sid) {
        Stall stall = stallMapper.selectById(sid);

        if (stall == null) return new ResultVO<>().message("该车位不存在").flag(false);

        if(SaFoxUtil.isEmpty(stall.getUserId())){
            stall.setUserId(uid);
            stall.setStallState("已停车");
            stallMapper.updateById(stall);

            StallRes stallRes = new StallRes();
            User user = userMapper.selectById(uid);
            stallRes.setPerson(user.getUsername());
            stallRes.setStallId(sid);
            stallRes.setCreateTime(LocalDateTime.now());
            stallResMapper.insert(stallRes);

            return new ResultVO<>().message("预约成功").flag(true);
        }


        return new ResultVO<>().message("该用户没有预约权限").flag(false);
    }

    @Override
    public List<StallRes> getAllStallRes(String person) {
        return stallResMapper.getAllStallRes(person);
    }

    @Override
    public List<StallRes> getAllNoPay(String person) {
        return stallResMapper.getAllStallRes(person);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object payMoneyPerson(StallRes stallRes) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username",stallRes.getPerson()));

        if (!SaFoxUtil.isEmpty(user)) {
            user.setMoney(user.getMoney()-stallRes.getMoney());
            userMapper.updateById(user);

            stallMapper.setStallOrg(stallRes.getStallId());

            StallRes stallRes1 = new StallRes();
            stallRes1.setPid(stallRes.getPid());
            stallRes1.setOverTime(LocalDateTime.now());
            stallRes1.setMoney(stallRes.getMoney());
            stallResMapper.updateById(stallRes1);

            Recharge recharge = new Recharge();
            recharge.setMoney(stallRes.getMoney());
            recharge.setPerson(stallRes.getPerson());
            recharge.setCtime(LocalDateTime.now());
            rechargeMapper.insert(recharge);

            return new ResultVO<>().flag(true).message("支付成功");
        }
        return new ResultVO<>().flag(false).message("支付失败，用户不存在");
    }
}
